package logic

import (
	"context"
	"furniture/models"

	"furniture/furniturerpc/furniturerpc"
	"furniture/furniturerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowLogic {
	return &ShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowLogic) Show(in *furniturerpc.ShowRequest) (*furniturerpc.ShowResponse, error) {
	// todo: add your logic here and delete this line

	var furn models.Furn
	models.DB.Debug().Model(&models.Furniture{}).
		Select("furnitures.*, types.`name` as types_name,  brands.`name` as brand_name").
		Where("furnitures.id = ?", in.FurnitureId).
		Joins("LEFT JOIN types on furnitures.types_id = types.id").
		Joins("LEFT JOIN brands on furnitures.brand_id = brands.id").
		Find(&furn)

	return &furniturerpc.ShowResponse{
		Name:        furn.Name,
		Price:       float32(furn.Price),
		Address:     furn.Address,
		TypesName:   furn.TypesName,
		BrandName:   furn.BrandName,
		FurnitureId: int64(furn.ID),
	}, nil
}
